<?php

class Control_Model_Table_Login extends Zend_Db_Table_Abstract {

    protected $_name = 'users';

    public function login($login, $password) {
        $login = stripcslashes(strip_tags(trim($login)));
        $password = stripcslashes(strip_tags(trim($password)));
        $select = $this->select();
        $select->where('user=?', $login)
                ->order('id DESC')
                ->limit('1');
        $Data = $this->_db->query($select)->fetchAll();
        if ($Data['0']['password'] == md5(md5($password))) {
            $hash = rand(0, 9999999);
            $_SESSION['user_pw'] = substr(md5($Data['0']['password']), 5);
            $_SESSION['usr'] = $Data['0']['id'];
            $_SESSION['name'] = $Data['0']['user'];
            $_SESSION['hash'] = $hash;
            $this->update(array('hash' => $hash), 'id=' . $Data['0']['id']);
        } else {
            echo '<a href="/control/index/index">Relogin</a>';
        }
    }

    public function chek() {
        $select = $this->select();
        $select->where('id=?', $_SESSION['usr'])
                ->order('id DESC')
                ->limit('1');
        $Data = $this->_db->query($select)->fetchAll();
        if (($_SESSION['user_pw'] == substr(md5($Data['0']['password']), 5)) && ($_SESSION['hash'] == $Data['0']['hash'])) {
            $hash = rand(0, 9999999);
            $_SESSION['hash'] = $hash;
            $this->update(array('hash' => $hash), 'id=' . $_SESSION['usr']);
        } else {
            echo '<a href="/control/index/index">Relogin</a> (Anti-atack)';
            unset($_SESSION['user_pw']);
            unset($_SESSION['hash']);
            exit();
        }
    }

    public function logout() {
        unset($_SESSION['user_pw']);
        unset($_SESSION['hash']);
        unset($_SESSION['usr']);
        unset($_SESSION['name']);
        echo '<a href="/control/index/index">Relogin</a>';
    }

    public function getUser($id) {
        $id = (int) $id;
        $row = $this->fetchRow('id = ' . $id);
        if (!$row) {
            throw new Exception("Count not find row $id");
        }
        return $row->toArray();
    }

    public function getAllUsers() {
        $select = $this->select();
        $select->order('id ASC');
        $Data = $this->_db->query($select)->fetchAll();
        if (!$Data) {

        } else {
            return $Data;
        };
    }

    public function deleteUser($id) {
        if ($id > 1) {
            $this->delete('id =' . (int) $id);
        }
    }

    public function addUser($user, $password, $name, $soname) {
        $password = md5($password);
        $data = array(
            'user' => $user,
            'password' => $password,
            'Name' => $name,
            'Soname' => $soname
        );
        $this->insert($data);
    }

    public function updateUser($id, $user, $password, $name, $soname) {

        if ($password == '') {
            $data = array(
                'user' => $user,
                'Name' => $name,
                'Soname' => $soname
            );
        } else {
            $password = md5($password);
            $data = array(
                'user' => $user,
                'password' => $password,
                'Name' => $name,
                'Soname' => $soname
            );
        }
        $this->update($data, 'id='.(int)$id);
    }

}